#include "YGWindowWin32.h"

namespace YoghurtGum
{

	HINSTANCE    WindowWin32::s_Instance;
	WNDCLASS     WindowWin32::s_WindowClass;
	HWND         WindowWin32::s_Handle;
	PAINTSTRUCT  WindowWin32::s_Canvas;
	RECT         WindowWin32::s_Size;
	MSG          WindowWin32::s_MSG;

	LRESULT WINAPI WindowWin32::Messages(HWND a_Handle, UINT a_Message, WPARAM wParam, LPARAM lParam)
	{
		switch(a_Message)
		{
		case WM_KEYDOWN:
			Keyboard::Get().InjectPress(wParam);
			break;

		case WM_KEYUP:
			Keyboard::Get().InjectRelease(wParam);
			break;

		case WM_LBUTTONDOWN:
			TouchScreen::Get().InjectPress((float)LOWORD(lParam), (float)HIWORD(lParam));
			break;

		case WM_LBUTTONUP: 
			TouchScreen::Get().InjectRelease(0.f, 0.f);
			break;

		case WM_PAINT:
			BeginPaint(s_Handle, &s_Canvas);
			EndPaint(s_Handle, &s_Canvas);
			break;

		case WM_CLOSE:
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		}

		return DefWindowProc(a_Handle, a_Message, wParam, lParam);
	}

	WindowWin32::WindowWin32()
	{

	}

	WindowWin32::~WindowWin32()
	{

	}

	void WindowWin32::Init()
	{
		//Log::GetSingleton() << "Setting up window.\n";

		s_Instance = (HINSTANCE)GetForegroundWindow();

		HICON icon = LoadIcon(NULL, L"SeaMonster.ico");
		if (!icon)
		{
			Error::LastError();
			ERROR_EXPLAIN("Could not load the icon.");
		}

		// register the window class
		WNDCLASS temp = { 
			0,               // defines the class style 
			Messages,        // pointer to the message handler
			0,               // extra bytes following the window class structure
			0,               // extra bytes following the window instance
			s_Instance,      // instance handle				
			icon,            // icon handle
			NULL,            // cursor
			NULL,            // background brush
			NULL,            // menu name
			YG_CLASS         // window title
		};
		s_WindowClass = temp;
		RegisterClass(&s_WindowClass);

		// create window
		s_Handle = CreateWindow (
			YG_CLASS,	                  // class name
			m_Title,                      // window title
			WS_VISIBLE | WS_POPUP,        // visibility settings
			0,                            // window x
			0,                            // window y
			Device::Get().GetWidth(),     // window width
			Device::Get().GetHeight(),    // window height
			NULL, 
			NULL, 
			s_WindowClass.hInstance, 
			NULL 
			);

		ZeroMemory(&s_MSG, sizeof(s_MSG));

		m_Handle = s_Handle;
	}

	void WindowWin32::SetName(const char* a_Name)
	{
		mbstowcs(m_Title, a_Name, YG_MAX_PATH);
	}

	bool WindowWin32::Update()
	{
		if (PeekMessage(&s_MSG, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&s_MSG);
			DispatchMessage(&s_MSG);
			return false;
		}

		return true;
	}

	bool WindowWin32::PreRender()
	{
		ShowWindow(s_Handle, SW_SHOWNORMAL);
		UpdateWindow(s_Handle);

		return true;
	}

	bool WindowWin32::PostRender()
	{
		return true;
	}

	bool WindowWin32::Close()
	{
		return true;
	}

	void* WindowWin32::GetProperty(const char* a_Name)
	{
		if (!strcmp(a_Name, "WindowHandle"))
		{
			return &m_Handle;
		}

		return NULL;
	}

}